// 激流勇进页面逻辑：获取排队人数和项目状态
(function () {
    var isLoading = false;
    
    function updateQueueInfo() {
        if (isLoading) return;
        isLoading = true;
        
        showLoadingState('queue');
        
        // 获取排队人数和项目状态
        apiService.getRollerCoasterQueue()
            .then(function (data) {
                var queueCount = data.queueCount || data.queue_count || 0;
                var waitTime = data.waitTime || data.wait_time || 0;
                var status = data.status || 'running';
                
                // 更新排队人数
                var countEl = document.getElementById('queueCount');
                if (countEl) {
                    countEl.textContent = queueCount + '人';
                    countEl.className = 'queue-number ' + getQueueLevel(queueCount);
                }
                
                // 更新等待时间
                var timeEl = document.getElementById('waitTime');
                if (timeEl) {
                    timeEl.textContent = waitTime + '分钟';
                    timeEl.className = 'queue-time ' + getWaitLevel(waitTime);
                }
                
                // 更新项目状态
                var statusEl = document.getElementById('projectStatus');
                if (statusEl) {
                    var statusText = getStatusText(status);
                    statusEl.textContent = statusText;
                    statusEl.className = 'project-status ' + status;
                }
                
                hideLoadingState('queue');
            })
            .catch(function (err) {
                console.error('获取排队信息失败:', err);
                hideLoadingState('queue');
                // 显示错误状态
                var countEl = document.getElementById('queueCount');
                var timeEl = document.getElementById('waitTime');
                var statusEl = document.getElementById('projectStatus');
                
                if (countEl) countEl.textContent = '获取失败';
                if (timeEl) timeEl.textContent = '获取失败';
                if (statusEl) statusEl.textContent = '服务异常';
            })
            .finally(function() {
                isLoading = false;
            });
    }
    
    function updateEquipmentStatus() {
        if (isLoading) return;
        isLoading = true;
        
        showLoadingState('equipment');
        
        // 获取道具租借状态
        apiService.getRollerCoasterEquipment()
            .then(function (data) {
                // 更新水枪状态
                var waterGunEl = document.getElementById('waterGunStatus');
                if (waterGunEl) {
                    var waterGunData = data.waterGun || data.water_gun || { available: 0, total: 0 };
                    updateEquipmentDisplay(waterGunEl, waterGunData);
                }
                
                // 更新雨衣状态
                var raincoatEl = document.getElementById('raincoatStatus');
                if (raincoatEl) {
                    var raincoatData = data.raincoat || { available: 0, total: 0 };
                    updateEquipmentDisplay(raincoatEl, raincoatData);
                }
                
                // 更新手机防水袋状态
                var phoneBagEl = document.getElementById('phoneBagStatus');
                if (phoneBagEl) {
                    var phoneBagData = data.phoneBag || data.phone_bag || { available: 0, total: 0 };
                    updateEquipmentDisplay(phoneBagEl, phoneBagData);
                }
                
                hideLoadingState('equipment');
            })
            .catch(function (err) {
                console.error('获取道具信息失败:', err);
                hideLoadingState('equipment');
                // 显示错误状态
                var waterGunEl = document.getElementById('waterGunStatus');
                var raincoatEl = document.getElementById('raincoatStatus');
                var phoneBagEl = document.getElementById('phoneBagStatus');
                
                if (waterGunEl) {
                    waterGunEl.textContent = '获取失败';
                    waterGunEl.className = 'equipment-status unavailable';
                }
                if (raincoatEl) {
                    raincoatEl.textContent = '获取失败';
                    raincoatEl.className = 'equipment-status unavailable';
                }
                if (phoneBagEl) {
                    phoneBagEl.textContent = '获取失败';
                    phoneBagEl.className = 'equipment-status unavailable';
                }
            })
            .finally(function() {
                isLoading = false;
            });
    }
    
    function updateEquipmentDisplay(element, data) {
        var available = data.available || data.available_count || 0;
        var total = data.total || data.total_count || 0;
        
        if (available > 0) {
            element.textContent = '可租借 (' + available + '/' + total + ')';
            element.className = 'equipment-status available';
        } else {
            element.textContent = '已租完';
            element.className = 'equipment-status unavailable';
        }
    }
    
    function getQueueLevel(count) {
        if (count <= 20) return 'low';
        if (count <= 50) return 'medium';
        return 'high';
    }
    
    function getWaitLevel(minutes) {
        if (minutes <= 15) return 'low';
        if (minutes <= 30) return 'medium';
        return 'high';
    }
    
    function getStatusText(status) {
        switch (status) {
            case 'running': return '运行中';
            case 'maintenance': return '维护中';
            case 'closed': return '已关闭';
            case 'busy': return '繁忙';
            default: return '运行中';
        }
    }
    
    // 显示加载状态
    function showLoadingState(type) {
        if (type === 'queue') {
            var queueItems = document.querySelectorAll('.queue-info');
            queueItems.forEach(function(item) {
                item.classList.add('loading');
            });
        } else if (type === 'equipment') {
            var equipmentItems = document.querySelectorAll('.equipment-item');
            equipmentItems.forEach(function(item) {
                item.classList.add('loading');
            });
        }
    }
    
    // 隐藏加载状态
    function hideLoadingState(type) {
        if (type === 'queue') {
            var queueItems = document.querySelectorAll('.queue-info');
            queueItems.forEach(function(item) {
                item.classList.remove('loading');
            });
        } else if (type === 'equipment') {
            var equipmentItems = document.querySelectorAll('.equipment-item');
            equipmentItems.forEach(function(item) {
                item.classList.remove('loading');
            });
        }
    }
    
    // 页面加载时获取信息
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function () {
            updateQueueInfo();
            updateEquipmentStatus();
            // 每30秒更新一次
            setInterval(updateQueueInfo, 30000);
            setInterval(updateEquipmentStatus, 30000);
        });
    } else {
        updateQueueInfo();
        updateEquipmentStatus();
        setInterval(updateQueueInfo, 30000);
        setInterval(updateEquipmentStatus, 30000);
    }
})();
